package org.wak.gui
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	
	import org.wak.action.ActionsManager;
	import org.wak.gui.events.GuiEvent;
	import org.wak.gui.modern.ModernGui;
	import org.wak.gui.sci.SciGui;
	import org.wak.gui.scumm.ScummGui;
	import org.wak.main.AdventureMaster;
	import org.wak.main.AdventureStyle;

	/**
	 * Manager of the Graphical user interface used in the adventure.  
	 * 
	 */	
	public class GuiManager extends EventDispatcher
	{
		private var mGuiLayer:Sprite = null;
		
		private var mCursorLayer:Sprite = null;
		
		private var mAdventureStyle:String = "" ; 
		
		private var mAdventureGui:IAdventureGui = null ;
		
		private var mGuiSettings:XML = null ;
		
		private var mActionsMngr:ActionsManager = null ;
		
		private var mEnabled:Boolean = true ;
		
		private var mHasMouse:Boolean = false ;
		
		
		
		/**
		 * Constructor 
		 * 
		 * @param pGuiLayer The Sprite layer of the game where to draw the GUI.
		 * @param pCursorLayer The Sprite layer of the game where to draw the mouse cursor.
		 * @param pAdventureStyle What kind of adventure style 
		 * 
		 */		
		public function GuiManager( pGuiLayer:Sprite,pCursorLayer:Sprite, pAdventureStyle:String)
		{
			super();
												
			mGuiLayer = pGuiLayer ;
			mCursorLayer = pCursorLayer ;
			mAdventureStyle = pAdventureStyle ;
			
		}
		
		/**
		 * Initializes the Graphic User interface Manager 
		 * 
		 * @param pGuiSettings an xml with the graphics settings for the selected GUI
		 */		
		public function Initialize(pGuiSettings:XML):void
		{
			mGuiSettings = pGuiSettings ;
			mAdventureGui = CreateGUI( mAdventureStyle );
			mActionsMngr = AdventureMaster.ActionsMngr ;
			SetEventListeners(mAdventureGui);
			mGuiLayer.addChild( mAdventureGui.display );
			
			if(AdventureMaster.useCustomCursors )
				mAdventureGui.SetMouseCursor( mCursorLayer );
			
			mAdventureGui.display.stage.addEventListener(Event.MOUSE_LEAVE,OnMouseLeave);
			mAdventureGui.display.stage.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
			
			
			
		}
		
		protected function OnMouseLeave(event:Event):void
		{
			mHasMouse = false ;	
			if(AdventureMaster.useCustomCursors )
			{
				Mouse.show() ;
				if(AdventureMaster.useCustomCursors )
					mAdventureGui.mouseCursor.visible = false ;
			}
		}
		
		protected function OnMouseMove(event:Event):void
		{
			if(!mHasMouse)
			{
				mHasMouse = true ;
				mAdventureGui.mouseCursor.visible = true ;
				if(AdventureMaster.useCustomCursors )
				{
					Mouse.hide();
				}
			}
		}
		
		/**
		 * The current IAdventureGui. 
		 * @return 
		 * 
		 */		
		public function get adventureGui():IAdventureGui
		{
			return mAdventureGui ;
		}
		
		public function set enabled(value:Boolean):void
		{
			mEnabled = value ;
			
			if(!mEnabled)
			{
				mAdventureGui.enabled = false ;
			}
			else
			{
				mAdventureGui.enabled = true ;
			}
		}
		
		public function get enabled():Boolean
		{
			return mEnabled ;	
		}
		
		private function SetEventListeners( pAdventureGui:IAdventureGui):void
		{
			pAdventureGui.addEventListener( GuiEvent.ACTION_SELECTED, OnActionSelected );
			pAdventureGui.addEventListener( GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemSelected );
			pAdventureGui.addEventListener( GuiEvent.INVENTORY_ITEM_OVER, OnInventoryItemMouseOver );
			pAdventureGui.addEventListener( GuiEvent.INVENTORY_ITEM_OUT, OnInventoryItemMouseOut )
		}
		
		private function RemoveEventListeners( pAdventureGui:IAdventureGui):void
		{
			pAdventureGui.removeEventListener( GuiEvent.ACTION_SELECTED, OnActionSelected );
			pAdventureGui.removeEventListener( GuiEvent.INVENTORY_ITEM_SELECTED, OnInventoryItemSelected );
			pAdventureGui.removeEventListener( GuiEvent.INVENTORY_ITEM_OVER, OnInventoryItemMouseOver );
			pAdventureGui.removeEventListener( GuiEvent.INVENTORY_ITEM_OUT, OnInventoryItemMouseOut )
		}
		
		private function CreateGUI(pAdventureStyle:String):IAdventureGui
		{
			var fRetGui:IAdventureGui = null ;
			
			switch(pAdventureStyle)
			{
				case AdventureStyle.ScummStyle:
				fRetGui = new ScummGui(mGuiSettings);
				break
				case AdventureStyle.SCIStyle:
				fRetGui = new SciGui(mGuiSettings);
				break;
				case AdventureStyle.ModernStyle:
				fRetGui = new ModernGui(mGuiSettings);
				break;
			}
			
			return fRetGui ;
		}
		
		private function OnActionSelected(event:GuiEvent):void
		{
			//trace("GuiManager OnActionSelected");
			var fEvent:GuiEvent = new GuiEvent(event.type, event.buttonId,event.inventoryItem );
			this.dispatchEvent(fEvent);
		}
		
		private function OnInventoryItemSelected(event:GuiEvent):void
		{
			//trace("GuiManager OnInventoryItemSelected");
			var fEvent:GuiEvent = new GuiEvent(event.type, event.buttonId, event.inventoryItem );
			this.dispatchEvent(fEvent);
		}
		
		private function OnInventoryItemMouseOver(event:GuiEvent):void
		{
			//trace("GuiManager OnInventoryItemMouseOver");
			var fEvent:GuiEvent = new GuiEvent(event.type, event.buttonId, event.inventoryItem );
			this.dispatchEvent(fEvent);
		}
		
		private function OnInventoryItemMouseOut(event:GuiEvent):void
		{
			//trace("GuiManager OnInventoryItemMouseOut");
			var fEvent:GuiEvent = new GuiEvent(event.type, event.buttonId, event.inventoryItem );
			this.dispatchEvent(fEvent);
		}
		
	}
}